/**
 * 表单控件管理工具
 */
;( function( global, factory ) {

	"use strict";

	if ( typeof define === "function" && define.amd ) {
		
		/*define( ["config"].concat(componentList.map((val) => {return "components/"+val;})), function(){
			return factory(global, arguments);
		} );*/
		define( () => factory(global) );
	} else {
		factory(global);
	}
} )( typeof window !== "undefined" ? window : this, function( window ) {
	//console.log(window);
	
	let componentsDefineCache = {};//组件信息缓存
	
	let initialize = (args) => {
		//初始化表单控件定义信息
		let i, len = args.length;
		for(i = 0; i < len; i++) {
			var obj = args[i];
			let cname = obj.name, conf = obj.conf, def = obj.def;
			if(conf.enable !== false) {
				window[cname] = def;
				//将组件加入到左侧拖拽表单基本元素列表
				add2BasicComponentList(cname, conf);
				componentsDefineCache[cname] = obj;
			}
		}
	}
	
	//将组件加入到左侧拖拽表单基本元素列表
	let add2BasicComponentList = (cname, conf) => {
		let li = document.getElementById("basic-components-li");
		let labelName = conf.label;
		let largeFlag = '';
		
		if(!li) {
			return;
		}
		
		if(typeof labelName === 'string' && labelName.length > 5) {
			largeFlag = 'large';
		}
		let html = '';
			html += '<div class="lyrow ui-draggable draggable-element basic-element ' + largeFlag + '" data-conp-name="'+ cname +'">';
			html += ' <div class="text-ellipsis" title="' + labelName +'">';
			html += '  <i class="glyphicon ' + conf.glyphicon + '"></i> ' + labelName;
			html += ' </div>';
			html += '</div>';
		//let ins = new compDef();
		//let domDrag = ins.dragableDom();
		//li.appendChild(domDrag);
		li.innerHTML += html;
		//console.log("write html.");
	}
	
	//将模型属性加入到左侧拖拽表单元素列表
	let addProperty2DragList = (property) => {
		let li = document.getElementById("model-field-li");
		if(!li) {
			return;
		}
		let cname = MODEL_PROPERTY_TYPE_2COMPONENT_TYPE_MAP[property.referenceValue];
		let conf;
		if(!cname || cname == "TODO") {
			cname = "FormInput";//默认使用单文本输入框
		}
		
		conf = componentsDefineCache[cname].conf;
		
		let labelName = property.propertyName;
		let largeFlag = '';
		if(typeof labelName === 'string' && labelName.length > 5) {
			largeFlag = 'large';
		}
		
		let html = '';
			html += '<div class="lyrow ui-draggable draggable-element model-property ' + largeFlag + '" data-conp-name="'+ cname
				+'" data-property-id="' + property.id + '" data-property-code="' + property.propertyCode + '">';
			html += ' <div class="text-ellipsis" title="' + labelName +'">';
			html += '  <i class="glyphicon ' + conf.glyphicon + '"></i> ' + labelName;
			html += ' </div>';
			html += '</div>';
		//let ins = new compDef();
		//let domDrag = ins.dragableDom();
		
		//li.appendChild(domDrag);
		li.innerHTML += html;
		//console.log("write html.");
	}
	
	/**
	 * 管理模型字段
	 * @param {Object} list
	 */
	let modelPropertyCache = {};//模型属性缓存
	let manageModelProperies = function(list) {
		if(typeof list === 'object' && list.length) {
			for(let i in list) {
				let property = list[i];
				//console.log(property);
				property.__shown = false;
				modelPropertyCache[property.id] = property;
				addProperty2DragList(property);
				formElementManager.set(property.id, property);
			}
		} else {
			console.log("无模型字段");
		}
	}
	
	
	let formElementManager = (()=>{
		let emap = {};
		//let _id = 1;
		return {
			init: function(a) {
				if(this.initialed) {
					console.log("表单控件管理器已初始化，无需重复初始化。");
					return;
				}
				console.log(a);
				initialize(a);
			},
			manageModelProperies: manageModelProperies,
			getModelProperty: function(id){
				return modelPropertyCache[id];
			},
			iteratorModelProperty: function(callback) {
				//遍历模型属性
				for(let i in modelPropertyCache) {
					callback(modelPropertyCache[i]);
				}
			},
			/*getId: function(){
				return _id++;
			},*/
			get: function(id) {
				return emap[id];
			},
			set: function(id, obj) {
				emap[id] = obj;
			},
			getall: function() {
				return Object.values(emap);
			}
		}
	})();
	//console.log(formElementManager);
	return formElementManager;
});